package com.example.smart_city_0829.activity.lawyer_consultation;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.lawyer_onsultation.LawyerList;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;
import com.example.smart_city_0829.view.MaxSizeLv;

import java.util.Calendar;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * 律师列表页面
 * 搜索结果页面
 */
public class SearchResultActivity extends BaseActivity {

    private int pageNum = 1;

    private MaxSizeLv maxSizeLv;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_lawyer_search_result;
    }

    @Override
    protected void initView() {
        maxSizeLv = (MaxSizeLv) findViewById(R.id.maxSizeLv);

    }


    /**
     * 干饭去了 ^_^
     * 哇库哇库
     * 休息一下
     */

    @Override
    protected void initData() {
        final String key = getIntent().getStringExtra("keyName");

        api.getLawyerList(null,null,null,key,null).enqueue(new NetCallBack<LawyerList>() {
            @Override
            public void onResponse(Call<LawyerList> call, Response<LawyerList> response) {
                if (response.body() == null) {
                    return;
                }

                final List<LawyerList.RowsBean> rows = response.body().getRows();
                maxSizeLv.setAdapter(new LvAdapter(rows,context));

            }
        });
    }

    @Override
    protected void initEvent() {
        maxSizeLv.setOnItemClickListener((parent, view, position, id) -> {
            final LawyerList.RowsBean item = (LawyerList.RowsBean) parent.getAdapter().getItem(position);


        });
    }

    @Override
    protected String getActionBarTitle() {
        return "搜索结果";
    }

    static class LvAdapter extends CommonAdapter<LawyerList.RowsBean,CommonAdapter.CommListViewHolder2>{

        public LvAdapter(List<LawyerList.RowsBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(LawyerList.RowsBean data, int position, CommListViewHolder2 viewHolder, View convertView, Context context) {
            Glide.with(context)
                    .load(Constant.BASE_URL + data.getAvatarUrl())
                    .into(viewHolder.imageView);

            viewHolder.name.setText(String.valueOf(data.getName()));
            viewHolder.workYearsTv.setText("从业"+(Calendar.getInstance().get(Calendar.YEAR) - Integer.parseInt(data.getWorkStartAt().substring(0,4)))+"年");
            viewHolder.goodAtTv.setText("法律专长："+data.getLegalExpertiseName());
            viewHolder.serviceTimesTv.setText("咨询人数："+data.getServiceTimes());
            viewHolder.favorableRateTv.setText("好评率："+data.getFavorableRate());

            viewHolder.quesBt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final Intent intent = new Intent(context, LawyerDetailActivity.class);
                    intent.putExtra("lawyerData",data);
                    context.startActivity(intent);
                }
            });
        }

        @Override
        protected CommListViewHolder2 getViewHolder() {
            return new CommListViewHolder2();
        }

        @Override
        protected void initView(CommListViewHolder2 viewHolder, View convertView) {
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.workYearsTv = (TextView) convertView.findViewById(R.id.work_years_tv);
            viewHolder.goodAtTv = (TextView) convertView.findViewById(R.id.goodAt_tv);
            viewHolder.serviceTimesTv = (TextView) convertView.findViewById(R.id.serviceTimes_tv);
            viewHolder.favorableRateTv = (TextView) convertView.findViewById(R.id.favorableRate_tv);
            viewHolder.quesBt = (Button) convertView.findViewById(R.id.ques_bt);

        }

        @Override
        protected int getItemLayoutResourceId() {
            return R.layout.lawyer_list_item;
        }
    }
}